package com.zbkj.service.service.foster.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zbkj.common.model.foster.FosterPlan;
import com.zbkj.common.model.merchant.MerchantAddress;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.service.dao.foster.FosterPlanDao;
import com.zbkj.service.service.MerchantAddressService;
import com.zbkj.service.service.ProductService;
import com.zbkj.service.service.foster.FosterPlanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 代认养计划Service实现类
 * +----------------------------------------------------------------------
 * @author xiaolic
 * +----------------------------------------------------------------------
 */
@Slf4j
@Service
public class FosterPlanServiceImpl extends ServiceImpl<FosterPlanDao, FosterPlan> implements FosterPlanService {

    @Resource
    private FosterPlanDao fosterPlanDao;

    @Autowired
    private MerchantAddressService merchantAddressService;

    @Autowired
    private ProductService productService;

    /**
     * 分页查询代认养计划
     * @param merchantId 商户ID，传null则查询所有
     * @param pageParamRequest 分页参数
     * @return 分页数据
     */
    @Override
    public PageInfo<FosterPlan> getPageList(Integer merchantId, PageParamRequest pageParamRequest) {
        log.info("获取代认养计划分页列表，merchantId={}", merchantId);
        
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        
        LambdaQueryWrapper<FosterPlan> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (merchantId != null) {
            lambdaQueryWrapper.eq(FosterPlan::getMerchantId, merchantId);
        }
        lambdaQueryWrapper.orderByDesc(FosterPlan::getSort);
        lambdaQueryWrapper.orderByDesc(FosterPlan::getId);
        
        List<FosterPlan> fosterPlanList = fosterPlanDao.selectList(lambdaQueryWrapper);
        
        log.info("获取代认养计划分页列表成功，共查询到{}条数据", fosterPlanList.size());
        return new PageInfo<>(fosterPlanList);
    }

    /**
     * 根据商品ID查询代认养计划列表
     * @param productId 商品ID
     * @return 代认养计划列表
     */
    @Override
    public List<FosterPlan> getListByProductId(Integer productId) {
        log.info("根据商品ID查询代认养计划列表，productId={}", productId);
        
        LambdaQueryWrapper<FosterPlan> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FosterPlan::getProductId, productId);
        lambdaQueryWrapper.orderByDesc(FosterPlan::getSort);
        lambdaQueryWrapper.orderByDesc(FosterPlan::getId);
        
        List<FosterPlan> fosterPlanList = fosterPlanDao.selectList(lambdaQueryWrapper);

        if (CollUtil.isNotEmpty(fosterPlanList)) {
            FosterPlan fosterPlan = fosterPlanList.get(0);
            if (fosterPlan.getAddressId() != null && fosterPlan.getAddressId() > 0) {
                MerchantAddress address = merchantAddressService.getByIdAndIsDelAndIsShow(fosterPlan.getAddressId());
                fosterPlan.setAddress(address);
            }
        }
        
        log.info("根据商品ID查询代认养计划列表成功，共查询到{}条数据", fosterPlanList.size());
        return fosterPlanList;
    }

    /**
     * 根据商户ID查询代认养计划列表
     * @param merchantId 商户ID
     * @return 代认养计划列表
     */
    @Override
    public List<FosterPlan> getListByMerchantId(Integer merchantId) {
        log.info("根据商户ID查询代认养计划列表，merchantId={}", merchantId);
        
        LambdaQueryWrapper<FosterPlan> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FosterPlan::getMerchantId, merchantId);
        lambdaQueryWrapper.orderByDesc(FosterPlan::getSort);
        lambdaQueryWrapper.orderByDesc(FosterPlan::getId);
        
        List<FosterPlan> fosterPlanList = fosterPlanDao.selectList(lambdaQueryWrapper);
        if (fosterPlanList != null && fosterPlanList.size() > 0) {
            for (FosterPlan fosterPlan : fosterPlanList) {
                fosterPlan.setProduct(productService.getById(fosterPlan.getProductId()));
            }
        }
        
        log.info("根据商户ID查询代认养计划列表成功，共查询到{}条数据", fosterPlanList.size());
        return fosterPlanList;
    }
}
